const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin'); // html模板启用

const resolve = (filePath) => {
    return path.resolve(process.cwd(), `src/${filePath}`)
}

const router = require("./router_config.js")

class HtmlConfig {
    entryConfig() {
        let config = {}
        for (let [key, value] of Object.entries(router)) {
            config[key] = resolve(value['index'])
        }
        return config
    }

    pluginConfig() {
        let config = []
        for (let [key, value] of Object.entries(router)) {
            const chunk = value.modules ?? []
            const options = {
                filename: `${key}.html`,
                scriptLoading: 'blocking',
                inject: 'body',
                template: resolve(value['template']),
                title: value['title'],
                chunks: [key, ...chunk],
            }
            const htmlPlugin = new HtmlWebpackPlugin(options);
            config.push(htmlPlugin)
        }
        return config
    }
}

module.exports = HtmlConfig
